/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg8boxclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.Cleaner;

/**
 *
 * @author Acer
 */
public class Client {

    //Socket dan I/O yang diperlukan
    public Socket clientSocket = null;
    PrintWriter outClient = null;
    BufferedReader inClient = null;
    
    //HOST dan PORT
    public final static String HOST = "AS4810T";
    public final static int PORT = 4444;
    
    //Permasalahan Login
    public boolean isStillLogin = false;
    
    public Client(){
        InitClientSocket();
        InitOutIn();
    }
    
    public void InitClientSocket(){
        try {
            clientSocket = new Socket(HOST, PORT);
        } catch (UnknownHostException ex) {
            System.err.println("Unknown host : " + HOST);
            System.exit(-1);
        } catch (IOException ex) {
            System.err.println("Couldn't get I/O for the connection to: " + HOST);
            System.exit(-1);
        }
    }
    
    public void InitOutIn(){
        try {
            outClient = new PrintWriter(clientSocket.getOutputStream(), true);
            inClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void Login(String username,String password){
        isStillLogin = true;
//        outClient.println("Name : " + username);
//        outClient.println("Password : " + password);
    }
    
    public void Logout() throws IOException{
        outClient.close();
        inClient.close();
        clientSocket.close();
    }
    
    public void ReceiveSomething(){
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Client client = new Client();
        String inputString;
        
        while ((inputString=client.inClient.readLine())!=null){
            System.out.println("Dari server : " + inputString);
            client.outClient.println("ADUH");
        }
        
        
//        while ((inputString=client.inClient.readLine())!=null){
//            System.out.println(inputString);
//        }
        
//        while ((inputString=client.inClient.readLine())!=null){
//            System.out.println("Nilai masukan : " + inputString);
//        }
        
        client.Logout();
    }
}
